package com.kazimsoylu.jdbc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.kazimsoylu.jpa.model.Employee;
import com.kazimsoylu.utils.EmployeeRowMapper;

public class EmployeeDaoJdbcImpl implements EmployeeDao
{
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Map<String,Object>> getEmployeesQueryForList()
	{
		String query = "SELECT employeenumber,firstname,lastname FROM EMPLOYEES";
		List<Map<String,Object>> employeeList = jdbcTemplate.queryForList(query);		
		return employeeList;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Employee> getEmployeesRowMapper() 
	{
		String query = "SELECT * FROM EMPLOYEES";
		
		List<Employee> empList = jdbcTemplate.query( query , new RowMapper() 
		{
			@Override
			public Object mapRow(ResultSet result, int arg1) throws SQLException 
			{
				Employee e = new Employee();
				e.setEmployeeNumber( result.getInt( "employeenumber" ) );
				e.setFirstName( result.getString( "firstname" ) );
				e.setLastName( result.getString( "lastname" ) );
				
				return e;
			}
			
		});
		
		return empList;
	}
	
	@Override
	public List<Employee> getEmployeesRowMapperV2() 
	{
		String query = "SELECT * FROM EMPLOYEES";		
		List<Employee> empList = jdbcTemplate.query( query , new EmployeeRowMapper());
		
		return empList;
	}
	
	public String getEmployeeName(int employeeNumber)
	{
		String query = "SELECT firstName from employees where employeenumber=?";
		String name = jdbcTemplate.queryForObject( query , String.class, new Object[]{ employeeNumber });
		
		return name;
	}
	
	@Override
	public List<String> getDistinctJobTitles() 
	{
		String query = "select distinct(jobtitle) from employees";
		List<String> list = jdbcTemplate.queryForList( query , String.class);
		
		return list;
	}
	
	@Override
	public Employee getEmployee(int employeeNumber) 
	{
		String query = "select employeenumber,firstname,lastname from employees where employeenumber=?";
		Employee employee = jdbcTemplate.queryForObject( query , new Object[]{employeeNumber}, new EmployeeRowMapper());
		
		return employee;
	}
	
	@Override
	public int getEmployeeCount() 
	{
		return jdbcTemplate.queryForInt( "select count(*) from employees" );
	}
	
	

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}


}
